package com.cdxy.service.impl;

import com.cdxy.dmr.dataObject.AreaDO;
import com.cdxy.dmr.repository.AreaRepository;
import com.cdxy.model.dto.resp.AllAreaResp;
import com.cdxy.service.IAreaService;
import com.cdxy.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Service
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private AreaRepository areaRepository;

    @Override
    public Map<String, AllAreaResp> allArea() {
        List<AreaDO> oneGrade = areaRepository.listAllOneGrade();
        List<AreaDO> twoGrade = areaRepository.listAllTwoGrade();

        // todo 地域
        Map<String, AllAreaResp> allAreaRespList = new HashMap<>(oneGrade.size());
        Map<Integer, List<AreaDO>> listMap = twoGrade.stream().collect(Collectors.groupingBy(AreaDO::getParentId));
        for (AreaDO areaDO : oneGrade) {
            AllAreaResp allAreaResp = BeanUtil.copyPropertiesByFastJson(areaDO, AllAreaResp.class);
            List<AreaDO> areaDOList = listMap.get(areaDO.getId());
            List<AllAreaResp> areaResps = BeanUtil.copyPropertiesByFastJson(areaDOList, AllAreaResp.class);
            allAreaResp.setAllAreaRespList(areaResps);
            allAreaRespList.put(areaDO.getName(), allAreaResp);
        }
        return allAreaRespList;
    }
}
